﻿using System;
using System.Collections.Generic;
using System.Text;

namespace cici
{
    class Mvcview
    {
        private Mvcviewlist mvcviewlist = new Mvcviewlist();
        private Mvcviewcreate mvcviewcreate = new Mvcviewcreate();
        private Mvcviewdetail mvcviewdetail = new Mvcviewdetail();
        private Mvcviewupdate mvcviewupdate = new Mvcviewupdate();
        private Mvcviewdelete mvcviewdelete = new Mvcviewdelete();
        private Mvcviewheader mvcviewheader = new Mvcviewheader();
        private Mvcviewfooter mvcviewfooter = new Mvcviewfooter();

        public Dictionary<object, object> forColumns { get; set; }

        public Mvcview()
        {
            this.forColumns = new Dictionary<object,object>();
        }

        public Boolean createAll(string className)
        {
            Boolean status = false;

            mvcviewlist.forColumnsList = this.forColumns;
            mvcviewlist.createMvcviewlist(className);

            mvcviewcreate.forColumnsCreate = this.forColumns;
            mvcviewcreate.createMvcviewcreate(className);

            mvcviewdetail.forColumnsDetail = this.forColumns;
            mvcviewdetail.createMvcviewdetail(className);

            mvcviewupdate.forColumnsUpdate = this.forColumns;
            mvcviewupdate.createMvcviewupdate(className);
            
            mvcviewdelete.createMvcviewdelete(className);

            return status;
        }

        public Boolean createHeaderFooter(string menu)
        {
            Boolean status = false;

            mvcviewheader.createMvcviewheader();

            mvcviewfooter.templatefooter = menu;
            mvcviewfooter.createMvcviewfooter();
            
            return status;
        }
    }

    class Mvcviewheader : View
    {
        public Mvcviewheader()
        {
        }

        public Boolean createMvcviewheader()
        {
            return this.createView("headerfooter.header;");
        }

        protected override string templateView(string file_location, string viewName)
        {
            string template =
                "<html>\n" +
                "\t<head>\n" +
                "\t\t<title>What?</title>\n" +
                "\t\t<style type=\"text/css\">\n" +
                "\t\t\tbody,table{\n" +
                "\t\t\t\tfont-family:arial;\n" +
                "\t\t\t\tfont-size:11px;\n" +
                "\t\t\t}\n" +
                "\t\t\th1,h2,h3{\n" +
                "\t\t\t\tmargin:0px;\n" +
                "\t\t\t\tpadding:0px;\n" +
                "\t\t\t\tcolor:gray;\n" +
                "\t\t\t\tborder-bottom:1px solid silver;\n" +
                "\t\t\t\tpadding-bottom:10px;\n" +
                "\t\t\t}\n" +
                "\t\t\th2{\n" +
                "\t\t\t\tmargin:10px;\n" +
                "\t\t\t\tborder-bottom:0px;\n" +
                "\t\t\t\tpadding:0px;\n" +
                "\t\t\t}\n" +
                "\t\t\th3{\n" +
                "\t\t\t\tmargin-bottom:10px;\n" +
                "\t\t\t}\n" +
                "\t\t\t#cls{\n" +
                "\t\t\t\tclear:both;\n" +
                "\t\t\t}\n" +
                "\t\t\t#main{\n" +
                "\t\t\t\tbackground:#fafafa;\n" +
                "\t\t\t\twidth:900px;\n" +
                "\t\t\t\tmin-width:900px;\n" +
                "\t\t\t\tborder:1px solid #eaeaea;\n" +
                "\t\t\t\ttext-align:left;\n" +
                "\t\t\t\tpadding:10px;\n" +
                "\t\t\t}\n" +
                "\t\t\t#content{\n" +
                "\t\t\t\tmargin-top:10px;\n" +
                "\t\t\t}\n" +
                "\t\t\t#left{\n" +
                "\t\t\t\tfloat:left;\n" +
                "\t\t\t\tmin-width:600px;\n" +
                "\t\t\t\twidth:600px;\n" +
                "\t\t\t\toverflow:auto;\n" +
                "\t\t\t\tborder:1px solid silver;\n" +
                "\t\t\t\tpadding:4px;\n" +
                "\t\t\t\tbackground:#ffffff;\n" +
                "\t\t\t}\n" +
                "\t\t\t#right{\n" +
                "\t\t\t\tfloat:right;\n" +
                "\t\t\t\tmin-width:270px;\n" +
                "\t\t\t\toverflow:auto;\n" +
                "\t\t\t\twidth:270px;\n" +
                "\t\t\t\tborder:1px solid silver;\n" +
                "\t\t\t\tbackground:#ffffff;\n" +
                "\t\t\t}\n" +
                "\t\t\t#menu{\n" +
                "\t\t\t\tlist-style:none;\n" +
                "\t\t\t\tmargin:0px;\n" +
                "\t\t\t\tpadding:4px;\n" +
                "\t\t\t}\n" +
                "\t\t\t#menu li{\n" +
                "\t\t\t\tpadding:10px;\n" +
                "\t\t\t\tborder-top:1px solid silver;\n" +
                "\t\t\t}\n" +
                "\t\t\t#menu li a{\n" +
                "\t\t\t\ttext-decoration:none;\n" +
                "\t\t\t}\n" +
                "\t\t\t#formcreate{\n" +
                "\t\t\t\tmargin:4px;\n" +
                "\t\t\t}\n" +
                "\t\t\tinput,textarea{\n" +
                "\t\t\t\tpadding:4px;\n" +
                "\t\t\t\twidth:60%;\n" +
                "\t\t\t\tmargin-bottom:10px;\n" +
                "\t\t\t\tborder:1px solid gray;\n" +
                "\t\t\t}\n" +
                "\t\t\tinput[type='submit'],input[type='button'],input[type='reset']{\n" +
                "\t\t\t\tpadding:4px;\n" +
                "\t\t\t\twidth:auto;\n" +
                "\t\t\t\tmargin-bottom:10px;\n" +
                "\t\t\t}\n" +
                "\t\t\t#message,#message2{\n" +
                "\t\t\t\tpadding:10px;\n" +
                "\t\t\t\tfont-weight:bold;\n" +
                "\t\t\t}\n" +
                "\t\t\t#message2{\n" +
                "\t\t\t\tpadding-left:4px;\n" +
                "\t\t\t}\n" +
                "\t\t\t#tablelist{\n" +
                "\t\t\t\ttext-align:left;\n" +
                "\t\t\t\twidth:100%;\n" +
                "\t\t\t\toverflow:auto;\n" +
                "\t\t\t\tbackground:gray;\n" +
                "\t\t\t}\n" +
                "\t\t\t#tablelist thead *{\n" +
                "\t\t\t\tbackground:#fafafa;\n" +
                "\t\t\t\tpadding:4px;\n" +
                "\t\t\t\twidth:100%;\n" +
                "\t\t\t}\n" +
                "\t\t\t#tablelist tbody *{\n" +
                "\t\t\t\tpadding:4px;\n" +
                "\t\t\t\tbackground:#eaeaea;\n" +
                "\t\t\t\twidth:100%;\n" +
                "\t\t\t}\n" +
                "\t\t\ta{\n" +
                "\t\t\t\tcolor:#333;\n" +
                "\t\t\t}\n" +
                "\t\t\ta:hover{\n" +
                "\t\t\t\ttext-decoration:none;\n" +
                "\t\t\t\tcolor:brown;\n" +
                "\t\t\t}\n" +
                "\t\t\t#footer{\n" +
                "\t\t\t\tmargin-top:20px;\n" +
                "\t\t\t\tpadding-top:10px;\n" +
                "\t\t\t\tpadding-bottom:10px;\n" +
                "\t\t\t\tborder-top:1px solid silver;\n" +
                "\t\t\t\tfont-size:11px;\n" +
                "\t\t\t}\n" +
                "\t\t\t#optionstable{\n" +
                "\t\t\t\tpadding:6px;\n" +
                "\t\t\t\tpadding-right:4px;\n" +
                "\t\t\t\tpadding-left:4px;\n" +
                "\t\t\t}\n" +
                "\t\t</style>\n" +
                "\t</head>\n" +
                "\t<body>\n" +
                "\t\t<center>\n" +
                "\t\t\t<div id=\"main\">\n" +
                "\t\t\t\t<h1 id=\"tablename\">Table Name</h1>\n" +
                "\t\t\t\t<div id=\"content\">\n" +
                "\t\t\t\t\t<div id=\"left\">\n" +
                "<!-- Generated by CICI -->";

            return template;
        }
    }
    class Mvcviewfooter : View
    {
        public string templatefooter { get; set; }

        public Mvcviewfooter()
        {
            this.templatefooter = "";
        }

        public Boolean createMvcviewfooter()
        {
            return this.createView("headerfooter.footer;");
        }

        protected override string templateView(string file_location, string viewName)
        {
            string template = "\t\t\t\t\t</div>\n" +
            "\t\t\t\t\t<div id=\"right\">\n" +
            "\t\t\t\t\t\t<h2>Menu</h2>\n" +
            "\t\t\t\t\t\t<ul id=\"menu\">\n" +
            "\t\t\t\t\t\t\t" + this.templatefooter + "\n" +
            "\t\t\t\t\t\t</ul>\n" +
            "\t\t\t\t\t</div>\n" +
            "\t\t\t\t\t<div id=\"cls\"></div>\n" +
            "\t\t\t\t</div>\n" +
            "\t\t\t\t<div id=\"footer\">\n" +
            "\t\t\t\t\tPowered by : <a href=\"http://cici.codeplex.com/\">CICI</a>\n" +
            "\t\t\t\t</div>\n" +
            "\t\t\t</div>\n" +
            "\t\t</center>\n" +
            "\t</body>\n" +
            "</html>\n" +
            "<!-- Generated by CICI -->";

            return template;
        }
    }
    class Mvcviewlist : View
    {
        public Dictionary<object, object> forColumnsList { get; set; }
        private string viewNameClass { get; set; }

        public Mvcviewlist()
        {
            this.viewNameClass = "";
            this.forColumnsList = new Dictionary<object, object>();
        }

        public Boolean createMvcviewlist(string viewName)
        {
            this.viewNameClass = viewName;
            return this.createView(viewName + ".list;");
        }

        protected override string templateView(string file_location, string viewName)
        {
            int limit = 2;
            string columnHeader = "";
            foreach (KeyValuePair<object, object> pair in this.forColumnsList)
            {
                if (limit == 0) break;
                columnHeader += "\t\t\t<th>" + pair.Key + "</th>\n";
                limit--;
            }
            columnHeader += "\t\t\t<th>Action1</th>\n"+
                "\t\t\t<th>Action2</th>\n\t\t\t<th>Action3</th>\n";

            limit = 2;
            string columnContent = "\t\t\t\t\t\t<tr>\n";
            foreach (KeyValuePair<object, object> pair in this.forColumnsList)
            {
                if (limit == 0) break;
                columnContent += "\t\t\t\t\t\t\t<td>'.$value->" + pair.Key + ".'</td>\n";
                limit--;
            }
            columnContent += "\t\t\t\t\t\t\t<td><a href=\"'.base_url('/" + this.viewNameClass +
                "/detail/'.$value->id" + this.viewNameClass + ").'\">Detail</a></td>\n" +
                "\t\t\t\t\t\t\t<td><a href=\"'.base_url('/" + this.viewNameClass +
                "/update/'.$value->id" + this.viewNameClass + ").'\">Update</a></td>\n" +
                "\t\t\t\t\t\t\t<td><a href=\"'.base_url('/" + this.viewNameClass +
                "/delete/'.$value->id"+this.viewNameClass+").'\">Delete</a></td>\n"+
                "\t\t\t\t\t\t</tr>";

            string template =
                "<script type=\"text/javascript\">\n" +
                "\tdocument.title=\"" + this.viewNameClass + " > List\";\n" +
                "\tdocument.getElementById('tablename').innerHTML='" + this.viewNameClass + "';\n" +
                "</script>\n" +
                "<div id=\"optionstable\">\n" +
                "\t<a href=\"<?php echo base_url('/" + this.viewNameClass + "/');?>\">List</a>\n" +
                "\t<a href=\"<?php echo base_url('/" + this.viewNameClass + "/create');?>\" style=\"float:right\">Create</a>\n" +
                "\t<div id=\"cls\"></div>\n" +
                "</div>\n" +
                "<table id=\"tablelist\">\n" +
                "\t<thead>\n" +
                "\t\t<tr>\n" +
                "\t\t\t\n" + columnHeader + "\n" +
                "\t\t</tr>\n" +
                "\t</thead>\n" +
                "\t<tbody>\n" +
                "\t\t<?php\n" +
                "\t\t\tforeach($data as $key => $value){\n" +
                "\t\t\t\techo '\n" + columnContent + "';\n" +
                "\t\t\t};\n" +
                "\t\t?>\n" +
                "\t</tbody>\n" +
                "</table>" +
                "<!-- Generated by CICI -->";

            return template;
        }
    }
    class Mvcviewcreate : View
    {
        public Dictionary<object, object> forColumnsCreate { get; set; }
        private string viewNameClass { get; set; }

        public Mvcviewcreate()
        {
            this.viewNameClass = "";
            this.forColumnsCreate = new Dictionary<object, object>();
        }

        public Boolean createMvcviewcreate(string viewName)
        {
            this.viewNameClass = viewName;
            return this.createView(viewName + ".create;");
        }

        protected override string templateView(string file_location, string viewName)
        {
            string formContent = "<?php echo form_open(base_url('/" + this.viewNameClass + "/create'),array('id'=>'formcreate')); ?>\n";
            foreach (KeyValuePair<object, object> pair in this.forColumnsCreate)
            {
                formContent += "<dt><?php echo form_label('" + pair.Key + "', '" + pair.Key + "'); ?></dt>\n" +
                                "<dd><?php echo form_" + pair.Value + "('" + pair.Key + "'); ?></dd>\n";
            }
            formContent += "<dt></dt>\n" +
                           "<dd><?php echo form_submit('submit', 'Create'); ?></dd>\n" +
                           "<?php echo form_close(); ?>\n";

            string template =
                "<script type=\"text/javascript\">\n" +
                "\tdocument.title=\"" + this.viewNameClass + " > Create\";\n" +
                "\tdocument.getElementById('tablename').innerHTML='" + this.viewNameClass + "';\n" +
                "</script>\n" +
                "<div id=\"optionstable\">\n" +
                "\t<a href=\"<?php echo base_url('/" + this.viewNameClass + "/');?>\">List</a>\n" +
                "\t<a href=\"<?php echo base_url('/" + this.viewNameClass + "/create');?>\" style=\"float:right\">Create</a>\n" +
                "\t<div id=\"cls\"></div>\n" +
                "</div>\n" + 
                "<div id=\"message2\"><?php if(isset($data['pesan'])){echo $data['pesan'];}?></div>\n" +
                formContent +
                "<!-- Generated by CICI -->";

            return template;
        }
    }
    class Mvcviewdetail : View
    {
        public Dictionary<object, object> forColumnsDetail { get; set; }
        private string viewNameClass { get; set; }

        public Mvcviewdetail()
        {
            this.viewNameClass = "";
            this.forColumnsDetail = new Dictionary<object, object>();
        }

        public Boolean createMvcviewdetail(string viewName)
        {
            this.viewNameClass = viewName;
            return this.createView(viewName + ".detail;");
        }

        protected override string templateView(string file_location, string viewName)
        {
            string detailContent = "";
            foreach (KeyValuePair<object, object> pair in this.forColumnsDetail)
            {
                detailContent += "\t<dt>"+pair.Key+"</dt>\n" +
                           "\t<dd><b><?php echo $data[0]->" + pair.Key + ";?></b></dd>\n";
            }

            string template =
                "<script type=\"text/javascript\">\n" +
                "\tdocument.title=\"" + this.viewNameClass + " > Detail\";\n" +
                "\tdocument.getElementById('tablename').innerHTML='" + this.viewNameClass + "';\n" +
                "</script>\n" +
                "<div id=\"optionstable\">\n" +
                "\t<a href=\"<?php echo base_url('/" + this.viewNameClass + "/');?>\">List</a>\n" +
                "\t<a href=\"<?php echo base_url('/" + this.viewNameClass + "/create');?>\" style=\"float:right\">Create</a>\n" +
                "\t<div id=\"cls\"></div>\n" +
                "</div>\n" +
                "<div id=\"optionstable\">\n" +
                detailContent +
                "</div>\n" +
                "<div id=\"optionstable\">\n" +
                "\t<a href=\"<?php echo base_url('/" + this.viewNameClass + "/update/'.$data[0]->id" + this.viewNameClass + ");?>\">Update</a>\n" +
                "\t<a href=\"<?php echo base_url('/" + this.viewNameClass + "/delete/'.$data[0]->id" + this.viewNameClass + ");?>\" style=\"float:right\">Delete</a>\n" +
                "\t<div id=\"cls\"></div>\n" +
                "</div>\n" +
                "<!-- Generated by CICI -->";

            return template;
        }
    }
    class Mvcviewupdate : View
    {
        public Dictionary<object, object> forColumnsUpdate { get; set; }
        private string viewNameClass { get; set; }

        public Mvcviewupdate()
        {
            this.viewNameClass = "";
            this.forColumnsUpdate = new Dictionary<object, object>();
        }

        public Boolean createMvcviewupdate(string viewName)
        {
            this.viewNameClass = viewName;
            return this.createView(viewName + ".update;");
        }

        protected override string templateView(string file_location, string viewName)
        {
            string formContent = "<?php echo form_open(base_url('/" + this.viewNameClass + "/update/'.$data[0]->id" + this.viewNameClass + "),array('id'=>'formcreate')); ?>\n";
            foreach (KeyValuePair<object, object> pair in this.forColumnsUpdate)
            {
                formContent += "<dt><?php echo form_label('" + pair.Key + "', '" + pair.Key + "'); ?></dt>\n" +
                           "<dd><?php echo form_"+pair.Value+"('" + pair.Key + "',$data[0]->" + pair.Key + "); ?></dd>\n";
            }
            formContent += "<dt></dt>\n" +
                           "<dd><?php echo form_submit('submit', 'Update'); ?></dd>\n" +
                           "<?php echo form_close(); ?>\n";

            string template =
                "<script type=\"text/javascript\">\n" +
                "\tdocument.title=\"" + this.viewNameClass + " > Update\";\n" +
                "\tdocument.getElementById('tablename').innerHTML='" + this.viewNameClass + "';\n" +
                "</script>\n" +
                "<div id=\"optionstable\">\n" +
                "\t<a href=\"<?php echo base_url('/" + this.viewNameClass + "/');?>\">List</a>\n" +
                "\t<a href=\"<?php echo base_url('/" + this.viewNameClass + "/create');?>\" style=\"float:right\">Create</a>\n" +
                "\t<div id=\"cls\"></div>\n" +
                "</div>\n" +
                "<div id=\"message2\"><?php if(isset($data['pesan'])){echo $data['pesan'];}?></div>\n" +
                formContent +
                "<!-- Generated by CICI -->";

            return template;
        }
    }
    class Mvcviewdelete : View
    {
        private string viewNameClass { get; set; }

        public Mvcviewdelete()
        {
            this.viewNameClass = "";
        }

        public Boolean createMvcviewdelete(string viewName)
        {
            this.viewNameClass = viewName;
            return this.createView(viewName + ".delete;");
        }

        protected override string templateView(string file_location, string viewName)
        {
            string template =
                "<script type=\"text/javascript\">\n" +
                "\tdocument.title=\"" + this.viewNameClass + " > Delete\";\n" +
                "\tdocument.getElementById('tablename').innerHTML='" + this.viewNameClass + "';\n" +
                "\tfunction redirect(){\n" +
                "\t\tdocument.location=\"<?php echo base_url('/" + this.viewNameClass + "/');?>\";\n" +
                "\t};\n" +
                "\tsetTimeout('redirect()',2000);\n" +
                "</script>\n" +
                "<div id=\"optionstable\">\n" +
                "\t<a href=\"<?php echo base_url('/" + this.viewNameClass + "/');?>\">List</a>\n" +
                "\t<a href=\"<?php echo base_url('/" + this.viewNameClass + "/create');?>\" style=\"float:right\">Create</a>\n" +
                "\t<div id=\"cls\"></div>\n" +
                "</div>\n" +
                "<div id=\"message\">\n" +
                "\t<?php if(isset($data['pesan'])){echo $data['pesan'];}?>, <i>Will be transferred in 2 seconds</i>\n" +
                "</div>" +
                "<!-- Generated by CICI -->";

            return template;
        }
    }
}
